const fs = require("fs");
const { resolve } = require('path');

let reg = /<div class="job-detail-box">.*?<a.*?data-nick=".*?".*?target="_blank".*?href="(https:\/\/www.liepin.com.*?)".*?>/sg;

function getHref(url, city, key) {
    return new Promise(function(resolve, reject) {
        let url1 = url.slice(0, -5) + '.txt';
        // 创建一个可写流
        var ws = fs.createWriteStream(url1);
        ws.once("close", function() {
            resolve(`${city}——${key}职位链接提取成功`);
        })
        return new Promise((resolve, reject) => {
            fs.readFile(url, function(err, data) {
                if (!err) {
                    let res = data.toString();
                    let iterator = res.matchAll(reg);
                    let tmp;
                    while (!(tmp = iterator.next()).done) {
                        ws.write(tmp.value[1] + '\n');
                    }
                    ws.close();
                }
            })
        })
    })
}

module.exports = {
    getHref: getHref,
};